/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package service.system.impl;

import dao.system.SystemUserDao;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import model.system.SystemUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import service.system.SystemUserService;
import utility.StringUtils;
import utility.com.Md5;

/**
 *
 * @author Yusup Jauhari Shandi <in.visi>
 */
@Service("systemUserService")
@Transactional(readOnly=true)
public class SystemUserServiceImpl implements SystemUserService{
    @Autowired SystemUserDao systemUserDao;
    private SystemUser activeUser;
    
    @Transactional(readOnly=false)
    @Override
    public boolean save(SystemUser systemUser) {
        return  systemUserDao.save(systemUser);
    }

    @Override
    public boolean delete(SystemUser systemUser) {
        return systemUserDao.delete(systemUser);
    }

    @Override
    public SystemUser getSystemUser(int id) {
        return systemUserDao.getById(id);
    }

    @Override
    public SystemUser getSystemUser(String kode) {
        return systemUserDao.getByKode("user_name",kode);
    }

   
    @Override
    public List<SystemUser> getSystemUser() {
        return systemUserDao.getAll();
    }

    @Override
    public List<SystemUser> getSystemUser(int start, int num) {
        return systemUserDao.getAll(start, num);
    }

    @Override
    public List<SystemUser> getAll(int start, int num, String params, String orderBy) {
        return systemUserDao.getAll(start, num, params, orderBy);
    }

    @Override
    public SystemUser canLogin(String username, String password) {
        String param = " user_name = ".concat(utility.StringUtils.fmtSQLString(username)).concat(" and passwd = ")
                .concat(StringUtils.fmtSQLString(Md5.setMd5(password)));
        SystemUser x = systemUserDao.getByParam(param);
        return (x);
    }

    @Override
    public boolean changePassword(SystemUser systemUser) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public SystemUser getActiveUser() {
        return activeUser;
    }

    @Override
    public void setActiveUser(SystemUser systemUser) {
        activeUser = systemUser;
    }
    
    @Override
    public String getUserIdLog(){
        if (activeUser==null){
            throw new UnsupportedOperationException("Belum ada user yang login.");
        }
        else{
            SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return activeUser.getUserName().concat(";").concat(fmt.format(new Date()));
        }
            
    }
    
    
}
